mui.init();
//var old_back = mui.back;
//mui.back = function(){
//  	var indexPage = plus.webview.getLaunchWebview();
//  	plus.storage.setItem(KeyCfg.currClickTabId,"userAccount");
//  indexPage.show();
//  mui.fire(indexPage,'refresh',{});
//}

var inputZrl_IdPrefix = "zrl_";	/*折让率文本框ID前缀*/
var btna_IdPrefix = "btnRedeem_";/*赎回按钮ID前缀*/
var btnIds = [];/*赎回按钮ID数组*/
var zrlIdArr = [];/*折让率ID文本框数组*/
init();
function init(){
	initBtnListeners();
}
mui.plusReady(function(){
	
	var self = plus.webview.currentWebview();
	var loanRecordIds = self.loanRecordIds;
	var discounts = self.discounts;
	var sessionId =	plus.storage.getItem(KeyCfg.JSESSIONID);
	/*---- 初始化数据 ---*/
	(function initData(){
		var url = UrlConfig.mforeclosureInve_applyTodoing;
		var formDatas = {loanRecordIds:loanRecordIds,discounts:discounts};
		 MuiEventMgr.ajax(url,{pars:formDatas,
	  	      sfn : function(data){
		  		setVals(data);
		  	},ffn : function(err){
		  		mui.alert("获取数据失败：["+err+"]");
		  	}
		 });
	})();	
	 
	 
//	var targetPageId = plus.storage.getItem(KeyCfg.targetPageId);
//	if(!targetPageId){
//		targetPageId = plus.webview.all()[0].id;
//		plus.storage.setItem(KeyCfg.targetPageId,targetPageId);
//	}
//	var targetPageId = plus.webview.currentWebview().id;
//	plus.storage.setItem(KeyCfg.targetPageId,targetPageId);
//	MuiEventMgr.checkSecuity();/*安全认证*/
	if(btnIds && btnIds.length > 0) btnIds = [];
});
 
function initBtnListeners(){
	/*---- 同意复选框 ----*/
	var chkImg_iagree = CmwMcore.$('chkImg_iagree');
	//chk_iagree.checked = "true"; 
	MeventMgr.onTouchstart(chkImg_iagree,function(event,self){
		var className = chkImg_iagree.className;
		var checked = "";
		if(className =="cmw_checkbox_checked"){
			className = "cmw_checkbox_empty";
		}else{
			className = "cmw_checkbox_checked";
			checked = "checked";
		}
		CmwMcore.$('iagree').checked = checked;
		chkImg_iagree.className = className;
		return false;
	});
	
	/*---- 确定按钮 ----*/
	var btn_finish = CmwMcore.$('btn_finish');
	MeventMgr.onTouchstart(btn_finish,function(event,self){
		var dis=$("#btn_finish").attr("disabled")//判断是否有这属性
		if(dis)return; 
		if(!validForm()) return false;
		doFinish();
		return false;
	});
	
	var btn_close = CmwMcore.$('btn_close');
	MeventMgr.onTouchstart(btn_close,function(event,self){
		resetDatas();
		return false;
	});
	
	function validForm(){
		var jypassword =  $("#jypassword").val();
		if(!jypassword){
			mui.alert("请输入您的交易密码!");
			return false;
		}
		
		var vcode = $("#phoneCode").val();
		if(!vcode){
			mui.alert("请输入您手机收到的验证码!");
			return false;
		}
		
		if(!$("#iagree").is(":checked")){
			mui.alert("您还没有同意《来理财债权转让协议》！");
			return false;
		}
		return true;
	}
	
	function doFinish(){
		CmwMcore.disabled('btn_finish');
		var phoneCode = $("#phoneCode").val();
		var jypassword =  $("#jypassword").val();
		var userId = plus.storage.getItem(KeyCfg.userId);
		var _dataStr = $("#data").val();

		var formDatas = {userId:userId,phoneCode:phoneCode,jypassword:jypassword,data:_dataStr};
		var url = UrlConfig.mforeclosureInve_applyDoing;
		MuiEventMgr.ajax(url,{pars:formDatas,
		  	sfn : function(data){
		  		CmwMcore.enabled('btn_finish');
		  		var success = data.success;
	        		var msg = data.msg;
	        		if(!success){
	        			if(msg == "1"){
	        				msg = "手机验证码错误!";
	        			}else if(msg == "2"){
	        				msg = "交易密码不正确!";
	        			}else{
	        				msg = "交易出错，请联系管理员!";
	        			}
	        			mui.alert(msg);
	        		}else{
	        			mui.alert("你的赎回申请已提出，请耐心等待赎回结果。");
//	        			var win = plus.webview.getWebviewById(appid);
//	        			win.show();
//	        			win.evalJS("activeTabItemPage('invest','1');");
                        MuiEventMgr.openBySafe('redeem.html');
	        		}
		  	},ffn : function(){
		  		CmwMcore.enabled('btn_finish');
	        		mui.alert("交易出错，请联系管理员！");
		  	}
		});
	}

}


function resetDatas(){
	if(btnIds && btnIds.length > 0) btnIds = [];
	$("#phoneCode").val("");
	$("#jypassword").val("");
	$("#iagree").removeAttr("checked");
	var chkImg_iagree = CmwMcore.$('chkImg_iagree');
 	if(chkImg_iagree) chkImg_iagree.className = "cmw_checkbox_empty";

}


/**
 * 为赎回页面赋值 
 * @param {Object} data
 */
function setVals(data){
	var msg = data.msg;
	var feeStart = msg.feeStart; 
	var feeEnd = msg.feeEnd;
	var companyfee = msg.companyfee;
	var periodNow = msg.periodNow;
	var periodLeft = msg.periodLeft;
	var repossessedMoney_val = msg.repossessedMoney || 0;
	repossessedMoney_val = StringHandler.forDight(repossessedMoney_val,2);
	var interest_val = msg.interest || 0;
	interest_val = StringHandler.forDight(interest_val,2);
	var discountMoney_val = msg.discountMoney || 0;
	discountMoney_val = StringHandler.forDight(discountMoney_val,2);
	var fees_val = msg.fees || 0;
	fees_val = StringHandler.forDight(fees_val,2);
	var redemptionMoney_val = msg.redemptionMoney || 0;
	redemptionMoney_val = StringHandler.forDight(redemptionMoney_val,2);
	var formData = msg.data || '';
	var loanTitle_val = msg.loanTitle;
	var loanRecordIds_val = msg.loanRecordIds;
	var discounts_val = msg.discounts;
	var fromUserId_val = msg.fromUserId;
	if(formData){
		formData = JSON.stringify(formData);
	}
	
	repossessedMoney_val = CmwMcore.getThousandths(repossessedMoney_val);
	interest_val = CmwMcore.getThousandths(interest_val);
	discountMoney_val = CmwMcore.getThousandths(discountMoney_val);
	fees_val = CmwMcore.getThousandths(fees_val);
	redemptionMoney_val = CmwMcore.getThousandths(redemptionMoney_val);
	$("#repossessedMoney").text("¥"+repossessedMoney_val);
	$("#interest").text("¥"+interest_val);
	$("#discountMoney").text("¥"+discountMoney_val);
	$("#fees").text("¥"+fees_val);
	$("#redemptionMoney").text("¥"+redemptionMoney_val);
	$("#loanTitle_val").text(loanTitle_val);
	$("#loanRecordIds_val").text(loanRecordIds_val);
	$("#discounts_val").text(discounts_val);
	$("#fromUserId_val").text(fromUserId_val);
	$("#redemptionfeeStart").html(feeStart);
	$("#redemptionfeeEnd").html(feeEnd);
	$("#companyfee").html(companyfee);
	$("#periodNow").html(periodNow);
	$("#periodLeft").html(periodLeft);
	$("#data").val(formData);
}

var agree_xy = CmwMcore.$("agree_xy");
MeventMgr.onTouchstart(agree_xy, function(event, self) {
	var loanRecordId = $("#loanRecordIds_val").text();
	var discountRate = $("#discounts_val").text();
	var fromUserId = $("#fromUserId_val").text();
	var cfg = {
		pars: {
			loanRecordId: loanRecordId,
			discountRate: discountRate,
			fromUserId: fromUserId,
			fromType: 'redeem'
		}
	};
	MuiEventMgr.open('../protocol/tranProtocol.html', cfg);
	return false;
});
